﻿<?
//print_r($_REQUEST);
if($_REQUEST['act'] == "edit" && $_REQUEST['cmd'] == "true")
{
	$valuearray['c_name'] = $_POST['name1'];
	$valuearray['c_name2'] = $_POST['name2'];
	Qupdate("category",'',$valuearray," c_id=".$_REQUEST['c_id'] );
	exit("<script type=\"text/javascript\">window.location='?body=cate';</script>");
}
else if($_REQUEST['act'] == "add" && $_REQUEST['cmd'] == "true")
{
	
	$maxorder = Q1("select MAX(c_order) from category  ");
	$valuearray['c_name'] = $_POST['name1'];
	$valuearray['c_name2'] = $_POST['name2'];
	$valuearray['c_order'] = $maxorder[0]+1;
	$valuearray['c_ip'] = $_SERVER['REMOTE_ADDR'];
	$valuearray['c_parent'] = $_POST['c_parent'];
	$valuearray['c_status'] = $_POST['c_status'];
	Qinsert("category","",$valuearray);
}
else if($_REQUEST['swap1'] >= 1 && $_REQUEST['swap2'] >= 1)
{
		$r1 = Q1("select c_id,c_order from category  where c_id =   ".$_REQUEST['swap1']);
		$r2 = Q1("select c_id,c_order from category  where c_id =   ".$_REQUEST['swap2']);
		mysql_query("update category set c_order = '".$r2['c_order']."' where c_id = '".$r1['c_id']."'  ");
		mysql_query("update category set c_order = '".$r1['c_order']."' where c_id = '".$r2['c_id']."'  ");
}
else if($_REQUEST['delid'] >= 1) 
{
	mysql_query("DELETE FROM `category` WHERE `category`.`c_id` = '".$_REQUEST['delid']."' LIMIT 1");
}
?>



<?

$r = Q1("select * from category where c_id = '".$_REQUEST['c_id']."' ");
$cparent = Q1("select * from category where c_id = '".$_REQUEST['c_parent']."' ");
?>
<p>
<!--<input type="button" value="back" onclick="history.back();">-->
<?
		if(count($cparent) > 1){
			$showsub = "::: ".$cparent['c_name']." / ".$cparent['c_name2'];
			echo "<a style = 'cursor:pointer;' onclick = \"history.back();\">กลับไปหน้าที่แล้ว</a>";
			$havesub = false;
		}else{
			$showsub = "";
			$havesub = true;
		}
?>

<fieldset>

<legend>Categorys box  <?=$showsub?></legend>
<br>
<form action="" method="post" enctype="multipart/form-data" name="a1">
<?
$rx = Q1("select * from language limit 1");
if($rx[lang1]!="")
{
?>
	<?=$rx[lang1]?> <input name="name1" type="text" value="<?=$r['c_name']?>" size="70" style='float:right'/>
<?
}
?>
<br><br>
<?
if($rx[lang2]!="")
{
?>
	<?=$rx[lang2]?> <input name="name2" type="text" value="<?=$r['c_name2']?>" size="70" style='float:right'/>
<?
}
?>
&nbsp;&nbsp;
<br><br>
<input name="submit" type="submit" value="<?=btsubmit($r['c_id'])?>" />
<input name="cmd" type="hidden" value="true" />
<input name="c_id" type="hidden" value="<?=$r['c_id']?>" />
<input name="c_parent" type="hidden" value="<?=$_REQUEST['c_parent']?>" />
<input name="c_status" type="hidden" value="<? ($_REQUEST['c_parent'] == "")? print "0":print "1";?>" />
<input name="act" type="hidden" value="<? ($_REQUEST['act'] == "addsub" || $_REQUEST['act'] == "")? print "add":print $_REQUEST['act'];?>" />
</form>
</fieldset>
</p>


<p>
<?
if($_REQUEST['c_parent'] == ""){$where = " c_status = 0 ";}
else{$where = " c_parent = '".$_REQUEST['c_parent']."'  ";}
$k=0;
$r1 = Q2("select * from category where  ".$where."  order by c_order desc");
foreach($r1 as $r) {$orderingg[] = $r['c_id'];}
foreach($r1 as $r)
{
?>
<div><!--	[&nbsp;
        <? if($orderingg[($k-1)] > 0) { ?>
        <a href="?body=cate&page=<?=$_REQUEST['page']?>&swap1=<?=$orderingg[$k]?>&swap2=<?=($orderingg[($k-1)])?>"><img src="images/uparrow.png" width="10" height="10" alt="Move up" title="Move up"  <?=btswap()?> border="0" ></a> 					
		<? } ?>
        
        <? if($orderingg[($k+1)] > 0) { ?>
        <a href="?body=cate&page=<?=$_REQUEST['page']?>&swap1=<?=$orderingg[$k]?>&swap2=<?=($orderingg[($k+1)])?>"><img src="images/downarrow.png" width="10" height="10" alt="Move down" title="Move down"  <?=btswap()?> border="0" ></a>
		<? } ?>&nbsp;]
        
        &nbsp;<a href="?body=cate&c_parent=<?=$r['c_id']?>&act=addsub"><img src="images/mediamanager.png" width="15" height="15" alt="Add sub category" title="Add sub category"  <?=btswap()?> border="0" ></a>&nbsp;
        &nbsp;<a href="?act=edit&body=cate&c_id=<?=$r['c_id']?>"><img src="images/addedit.png" width="15" height="15" alt="Edit" title="Edit"  <?=btswap()?> border="0" ></a>&nbsp;
        <a href="?body=cate&c_id=<?=$r['c_id']?>&page=<?=$_REQUEST['page']?>&delid=<?=$r['c_id']?>"  onclick="javascript:if(confirm('Are you sure you want to delete')){return true;} else{return false;}" > <img src="images/cancel_f2.png" alt="Delete" title="Delete"  width="15" height="15" border="0" <?=btswap()?> > </a>
&nbsp;::::: <?=$r['c_name']?> | <?=$r['c_name2']?>&nbsp;
		-->
</div>
<?
$k++;
}


?>
<?
		//$where = " c_status = 0 ";
		echo showcattree2($where , "margin:0px 0px 0px 10px;" , "block" , "style = 'font-weight:bold; font-size:18px'",$havesub);
	?>
</p>